<?php


namespace App\Models\Marketing;


use App\Models\BaseModel;
use App\Models\Common\ConfigModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ShopAdvertisementModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = 'marketing_shop_advertisement';

    protected $fillable = [
        'id',
        'title',
        'status',
        'type',
        'channel',
        'jumpStatus',
        'cutDown',
        'showRule',
        'shopScope',
        'notLoginShow',
        'jumpType',
        'jumpRelId',
        'jumpBtnShow',
        'jumpLink',
        'startAt',
        'endAt',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
        'isDeleted'
    ];

    const TYPE_LAUNCH_APP = 1;
    const TYPE_MAP = [
        self::TYPE_LAUNCH_APP=>'闪屏广告'
    ];

    const CHANNEL_MERCHANT  = 1;
    const CHANNEL_OPERATION = 2;

    const CHANNEL_MAP = [
        self::CHANNEL_MERCHANT  => '夸父商家',
        self::CHANNEL_OPERATION => '夸父运营',
    ];

    const JUMP_STATUS_N = 0;
    const JUMP_STATUS_Y = 1;

    const JUMP_STATUS_MAP = [
        self::JUMP_STATUS_N => '否',
        self::JUMP_STATUS_Y => '是',
    ];

    const SHOW_RULE_EVERY_TIMES       = 1;
    const SHOW_RULE_DAILY_FIRST       = 2;
    const SHOW_RULE_DAILY_THREE_TIMES = 3;

    const SHOW_RULE_MAP = [
        self::SHOW_RULE_EVERY_TIMES       => '每次打开',
        self::SHOW_RULE_DAILY_FIRST       => '每天首次',
        self::SHOW_RULE_DAILY_THREE_TIMES => '每天3次',
    ];

    const NOT_LOGIN_SHOW_N = 0;
    const NOT_LOGIN_SHOW_Y = 1;

    const NOT_LOGIN_SHOW_MAP = [
        self::NOT_LOGIN_SHOW_N => '不显示',
        self::NOT_LOGIN_SHOW_Y => '显示',
    ];

    const JUMP_TYPE_INNER         = 1;
    const JUMP_TYPE_OUTSIDE       = 2;
    const JUMP_TYPE_ARTICLE       = 3;
    const JUMP_TYPE_FUNCTION_PAGE = 4;
    const JUMP_TYPE_OUTSIDE_APP   = 5;

    const JUMP_TYPE_MAP = [
        self::JUMP_TYPE_INNER         => '内部链接',
        self::JUMP_TYPE_OUTSIDE       => '外部链接',
        self::JUMP_TYPE_ARTICLE       => '文章(视频)',
        self::JUMP_TYPE_FUNCTION_PAGE => '功能页面',
        self::JUMP_TYPE_OUTSIDE_APP   => '三方app',
    ];

    const JUMP_BTN_SHOW_N = 0;//是否显示跳转按钮
    const JUMP_BTN_SHOW_Y = 1;
    const JUMP_BTN_SHOW_MAP = [
        self::JUMP_BTN_SHOW_N=>'否',
        self::JUMP_BTN_SHOW_Y=>'是'
    ];

    const STATUS_DISABLE = 0;
    const STATUS_ENABLE = 1;
    const STATUS_MAP = [
        self::STATUS_DISABLE=>'禁用',
        self::STATUS_ENABLE=>'启用'
    ];

    const SHOP_SCOPE_PART = 0;
    const SHOP_SCOPE_ALL = 1;
    const SHOP_SCOPE_MAP = [
        self::SHOP_SCOPE_PART=>'部分',
        self::SHOP_SCOPE_ALL=>'全部',
    ];


    public static function getInfo(int $shopId): array
    {
        $config = ConfigModel::getShopAdsConfig();
        $shopIds = $config['shopIds'] ?? [];

        if ($shopId && $shopIds && !in_array($shopId,$shopIds)){
            return [];
        }

        $resource = collect($config['resource'] ?? [])->map(function ($item) {
            return [
                'platform' => (int)$item['platform'],//android、ios
                'url'      => $item['url'],
                'type'     => (int)$item['type'],// video、image
            ];
        })->toArray();

        return [
            'id'           => (int)$config['id'],
            'channel'      => (int)$config['channel'],
            'title'        => $config['title'],
            'resource'     => $resource,
            'jumpStatus'   => (int)$config['jumpStatus'],
            'countDown'    => (int)$config['countDown'],
            'startAt'      => date('Y-m-d H:i:s', $config['startAt']),
            'endAt'        => date('Y-m-d H:i:s', $config['endAt']),
            'showRule'     => (int)$config['showRule'],
            'notLoginShow' => (int)$config['notLoginShow'],
            'jumpType'     => (int)$config['jumpType'],
            'jumpBtnShow'  => (int)$config['jumpBtnShow'],
            'jumpLink'     => $config['jumpLink']
        ];
    }

    public function resourceRel():HasMany
    {
        return $this->hasMany(ShopAdvertisementResourcesModel::class,'advId','id');
    }

    public function mappingRel():HasMany
    {
        return $this->hasMany(ShopAdvertisementMappingModel::class,'advId','id');
    }


}